package com.zy.mall.controller;

import com.google.gson.Gson;
import com.zy.mall.model.Result;
import com.zy.mall.model.Type;
import com.zy.mall.model.bo.GoodsBo;
import com.zy.mall.model.vo.TypeGoodsVo;
import com.zy.mall.service.GoodService;
import com.zy.mall.service.GoodServiceImpl;
import com.zy.mall.utils.FileUploadUtils;
import com.zy.mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/api/admin/goods/*")
public class GoodServlet extends HttpServlet {

    private GoodService goodService = new GoodServiceImpl();
    private Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI(); 
        String action = requestURI.replace("/api/admin/goods/","");
        if ("imgUpload".equals(action)) {
            imgUpload(request, response);
        }else if ("addGoods".equals(action)) {
            addGoods(request, response);
        }else if ("addType".equals(action)) {
            addType(request, response);
        }
    }

    private void addType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Type type = gson.fromJson(requestBody, Type.class);
        int result = goodService.addType(type);
        if (result == 1) response.getWriter().println(gson.toJson(Result.ok(0)));
        if (result == 10000) response.getWriter().println(gson.toJson(Result.error("该类目已存在")));
    }

    private void addGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        GoodsBo goodsBo = gson.fromJson(requestBody, GoodsBo.class);
        goodService.addGoods(goodsBo);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 新增商品图片
     * @param request
     * @param response
     */
    private void imgUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, Object> map = FileUploadUtils.parseRequest(request);
        String file = (String) map.get("file");
        String domain = (String) getServletContext().getAttribute("domain");
        response.getWriter().println(gson.toJson(Result.ok(domain + file)));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI(); 
        String action = requestURI.replace("/api/admin/goods/","");
        if("getType".equals(action)){
            getType(request, response);
        }else if ("getGoodsByType".equals(action)) {
            getGoodsByType(request, response);
        }

    }

    /**
     * 获取某个分类下的商品信息
     * @param request
     * @param response
     */
    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeId = request.getParameter("typeId");
        List<TypeGoodsVo> goodsVOS = goodService.getGoodsByType(typeId);
        response.getWriter().println(gson.toJson(Result.ok(goodsVOS)));
    }


    /**
     * 加载商品所有分类
     * @param request
     * @param response
     */
    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Type> typeList = goodService.getType();
        response.getWriter().println(gson.toJson(Result.ok(typeList)));
    }
}
